var db = iClass.db;
var config = iClass.config;
var UserCon = iClass.set("User", function(info) {
	this._id = UserCon.getUuid();
	//初始化用户信息
	this.setInfo(info);

	//初始化用户密码
	this._setPassword(info.password);

	//用户注册事件
	this.create_time = +new Date;

	db.insert(this.dbname, this);
	return this;
});
// User.prototype.__nojson_able_keys__ =  ["password"];
UserCon.login = function(user_info) {
	if (!user_info.name) {
		throw "用户名不可为空";
	}
	if (!user_info.password) {
		throw "密码不可为空";
	}
	user_info.name = user_info.name.toLowerCase();
	var mybey_user = db.find_one(UserCon.dbname, {
		name: user_info.name
	});
	if (!mybey_user) { //邮箱登录
		var mybey_user = db.find_one(UserCon.dbname, {
			email: user_info.name
		});
	}
	if (!mybey_user) {
		console.log("找不到用户名为：“", user_info.name, "”的用户！");
		throw "找不到指定用户名的用户";
	} else if (mybey_user.password !== $$.md5_2(user_info.password)) {
		console.log("密码错误！", user_info.password);
		throw "密码错误";
	} else {
		console.log(user_info.name, "登陆成功");
		return mybey_user;
	}
};

UserCon.findUser = function(search_name) {
	var user_list = db.find_all(UserCon.dbname).filterMap(function(user_info) {
		if (user_info.name.indexOf(search_name) !== -1) {
			return UserCon.getInstance(user_info);
		}
	});
	return user_list;
};